package com.tbeasy.financial.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

import com.tbeasy.financial.provider.DatabaseUtil.TableCategory;
import com.tbeasy.financial.provider.DatabaseUtil.TableTransaction;

public class DatabaseProvider extends ContentProvider {

	private static final int TRANSACTION_ITEM = 2;
	private static final int TRANSACTION_ITEM_ID = 3;
	private static final int CATEGORY_ITEM = 4;
	private static final int CATEGORY_ITEM_ID = 5;

	private DatabaseHelper databaseHelper;
	private SQLiteDatabase database;
	private int count;

	private static final UriMatcher mUriMatcher;

	static {
		mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		mUriMatcher.addURI(DatabaseUtil.AUTHORITY, TableTransaction.TAB_NAME, TRANSACTION_ITEM);
		mUriMatcher.addURI(DatabaseUtil.AUTHORITY, TableTransaction.TAB_NAME + "/#", TRANSACTION_ITEM_ID);
		mUriMatcher.addURI(DatabaseUtil.AUTHORITY, TableCategory.TAB_NAME, CATEGORY_ITEM);
		mUriMatcher.addURI(DatabaseUtil.AUTHORITY, TableCategory.TAB_NAME + "/#", CATEGORY_ITEM_ID);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		database = databaseHelper.getReadableDatabase();
		switch (mUriMatcher.match(uri)) {

		case TRANSACTION_ITEM:

			break;

		case TRANSACTION_ITEM_ID:

			break;

		case CATEGORY_ITEM:

			break;

		case CATEGORY_ITEM_ID:

			break;

		default:
			throw new IllegalArgumentException("Unknow uri: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		database = databaseHelper.getWritableDatabase();
		long rowId = 0;
		switch (mUriMatcher.match(uri)) {

		case TRANSACTION_ITEM:
			rowId = database.insert(TableTransaction.TAB_NAME, null, values);
			if (rowId > 0) {
				Uri noteUri = ContentUris.withAppendedId(TableTransaction.CONTENT_URI, rowId);
				getContext().getContentResolver().notifyChange(noteUri, null);
				return noteUri;
			} else {
				throw new IllegalArgumentException("Unknown URI" + uri);
			}

		case TRANSACTION_ITEM_ID:

			break;

		case CATEGORY_ITEM:

			break;
		case CATEGORY_ITEM_ID:

			break;

		default:
			break;
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		boolean flag = true;
		try {
			databaseHelper = new DatabaseHelper(getContext());
		} catch (Exception e) {
			flag = false;
			throw new IllegalArgumentException("Error: onCreate of DataBaseHelper");
		}
		return flag;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		database = databaseHelper.getReadableDatabase();
		Cursor cursor = null;
		switch (mUriMatcher.match(uri)) {
		case TRANSACTION_ITEM:
			cursor = database.query(TableTransaction.TAB_NAME, projection, selection, selectionArgs, null, null, sortOrder);
			break;

		case TRANSACTION_ITEM_ID:
			 String id = uri.getPathSegments().get(1);
			 cursor = database.query(TableTransaction.TAB_NAME, projection, TableTransaction.TRANSACTION_ID+"="+id+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""),selectionArgs, null, null, sortOrder);
			break;

		case CATEGORY_ITEM:
			cursor = database.query(TableCategory.TAB_NAME, projection, selection, selectionArgs, null, null, sortOrder);
			break;

		case CATEGORY_ITEM_ID:

			break;

		default:
			throw new IllegalArgumentException("Unknow uri: " + uri);
		}
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		database = databaseHelper.getWritableDatabase();
		int result = 0;

		switch (mUriMatcher.match(uri)) {
		case TRANSACTION_ITEM:
			result = database.update(TableTransaction.TAB_NAME, values, selection, selectionArgs);
			break;

		case TRANSACTION_ITEM_ID:

			break;

		case CATEGORY_ITEM:
			result = database.update(TableCategory.TAB_NAME, values, selection, selectionArgs);
			break;

		case CATEGORY_ITEM_ID:

			break;

		default:
			break;
		}
		return result;
	}

}
